/*
 * File     : X86_64UnixNativeCallFactory.java
 * Created  : 21 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.call;

import java.util.List;
import java.util.logging.Level;

import com.googlecode.dni.internal.DniInternal;
import com.googlecode.dni.internal.library.DynamicLibrary;
import com.googlecode.dni.type.Pointer;


/**
 * <p>
 *  Implementation of {@link NativeCallFactory} that produces native methods for
 *  calling X86 stdcall Windows methods from X86 Windows.
 * </p>
 *
 * @author Matthew Wilson
 */
public final class X86StdCallWindowsNativeCallFactory extends BaseNativeCallFactory
{

    private static final DynamicLibrary LIBRARY;

    /** JNI function for general case. */
    private static final Pointer CALL;


    static
    {
        LIBRARY = DynamicLibrary.open( "dni.dll" );
        try
        {
            CALL = LIBRARY.getSymbol( "dni_x86_stdcall_windows_call" );
        }
        catch ( Throwable throwable )
        {
            try
            {
                LIBRARY.free();
            }
            catch ( Throwable throwable2 )
            {
                DniInternal.LOGGER.log( Level.SEVERE, "Failed to free library", throwable2 );
            }
            throw new ExceptionInInitializerError( throwable );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    Pointer getNativeBinding( final List< Parameter > returnParameterContainer,
                              final List< Parameter > parameters,
                              final Pointer functionPointer )
    {
        // TODO process var-args, lots of params, and structures-by-value

        parameters.add( 0, new FunctionPointerParameter( functionPointer ) );

        return CALL;
    }

}
